package com.qubi.exchange.util;

import com.qubi.exchange.message.OrderMessage;
import org.apache.commons.io.FileUtils;

import java.io.*;
import java.util.Comparator;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
/**
 * @description 压缩文件
 * @author SHOUSHEN LUAN
 * @DATE 2012-1-8下午03:10:01
 */
public class GZipUtils {


    /**
     * Member cache 文件解压处理
     *
     * @param buf
     * @return
     * @throws IOException
     */
    public static byte[] unGzip(byte[] buf) throws IOException {
        GZIPInputStream gzi = null;
        ByteArrayOutputStream bos = null;
        try {
            gzi = new GZIPInputStream(new ByteArrayInputStream(buf));
            bos = new ByteArrayOutputStream(buf.length);
            int count = 0;
            byte[] tmp = new byte[2048];
            while ((count = gzi.read(tmp)) != -1) {
                bos.write(tmp, 0, count);
            }
            buf = bos.toByteArray();
        } finally {
            if (bos != null) {
                bos.flush();
                bos.close();
            }
            if (gzi != null)
                gzi.close();
        }
        return buf;
    }

    /**
     * Member cache 文件压缩处理
     *
     * @param val
     * @return
     * @throws IOException
     */
    public static byte[] gzip(byte[] val) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(val.length);
        GZIPOutputStream gos = null;
        try {
            gos = new GZIPOutputStream(bos);
            gos.write(val, 0, val.length);
            gos.finish();
            gos.flush();
            bos.flush();
            val = bos.toByteArray();
        } finally {
            if (gos != null)
                gos.close();
            if (bos != null)
                bos.close();
        }
        return val;
    }

    /**
     * 对文件进行压缩
     *
     * @param source
     *            源文件
     * @param target
     *            目标文件
     * @throws IOException
     */
    public static void zipFile(String source, String target) throws IOException {
        FileInputStream fin = null;
        FileOutputStream fout = null;
        GZIPOutputStream gzout = null;
        try {
            fin = new FileInputStream(source);
            fout = new FileOutputStream(target);
            gzout = new GZIPOutputStream(fout);
            byte[] buf = new byte[1024];
            int num;
            while ((num = fin.read(buf)) != -1) {
                gzout.write(buf, 0, num);
            }
        } finally {
            if (gzout != null)
                gzout.close();
            if (fout != null)
                fout.close();
            if (fin != null)
                fin.close();
        }
    }

    /**
     * 解压文件

     * @throws IOException
     */
    public static void unZipFile(String source, String target)
            throws IOException {
        FileInputStream fin = null;
        GZIPInputStream gzin = null;
        FileOutputStream fout = null;
        try {
            fin = new FileInputStream(source);
            gzin = new GZIPInputStream(fin);
            fout = new FileOutputStream(target);
            byte[] buf = new byte[1024];
            int num;
            while ((num = gzin.read(buf, 0, buf.length)) != -1) {
                fout.write(buf, 0, num);
            }
        } finally {
            if (fout != null)
                fout.close();
            if (gzin != null)
                gzin.close();
            if (fin != null)
                fin.close();
        }
    }
    public static boolean isGZipped(byte[] f) {

        int b0 = f[0];
        int b1 = f[1];
        int b = (b1&0xFF)<<8 | b0;
        return b ==0x8b1f;
    }

    public static final Comparator<String> SORT_SELL = (o1, o2) -> {

        String h1=o1.substring(0,1);
        String h2=o2.substring(0,1);
        int cmp=h1.compareTo(h2);
        if(cmp==0) return o1.compareTo(o2);

        return cmp;
    };
    public static void main(String[] args) throws IOException {
        String content= FileUtils.readFileToString(new File("C:\\Users\\wxpker\\Desktop\\新建文本文档 (2).txt"),"UTF-8");


        String lines[]=content.replaceAll("\n","").split("\\[\\d+\\]");
        System.out.println(lines);
        var book = new ConcurrentSkipListMap<String, String>(SORT_SELL);

        for(String line:lines){
            if(line.startsWith(" ")){
                line=line.strip();
            }
            line=line.trim();
            if(line.length()>1)
            book.put(line,line);
        }

        int i=1;
        for(String l:book.keySet()){
            System.out.println("["+i++ +"] "+l);

        }

    }
}

